package cfca.seal.web.action.organization;

import cfca.seal.bean.AdminUser;
import cfca.seal.bean.OperationLog;
import cfca.seal.bean.Organization;
import cfca.seal.common.Page;
import cfca.seal.exception.CodeException;
import cfca.seal.mananger.OperationLogManager;
import cfca.seal.util.StringUtil;
import cfca.seal.web.service.OperationLogService;
import cfca.seal.web.service.OrganizationService;
import cfca.seal.web.service.SealModelService;
import cfca.seal.web.system.SessionManager;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping({"/organization"})
public class OrganizationAction
{
  private String jumpListPagePath = "sys/organization/list.html";
  private String jumpDetailPagePath = "sys/organization/detail.html";
  private String jumpEditPagePath = "sys/organization/edit.html";
  private String jumpAddPagePath = "sys/organization/add.html";
  private String jumplistdo = "redirect:/organization/v_list.do";
  private String applicationName = "机构管理";

  @Resource
  private OrganizationService organizationService;

  @Resource
  private OperationLogService operationLogService;

  @Resource
  private SealModelService sealImageService;
  private static final Logger logger = LoggerFactory.getLogger("web_sys");

  @RequestMapping({"/v_list"})
  public String getAll(String pageNo, ModelMap modelMap, HttpServletRequest request) throws CodeException {
    try {
      Page page = this.organizationService.getOrganizations(pageNo);
      modelMap.put("page", page);
      return this.jumpListPagePath;
    } catch (Exception e) {
      logger.error("机构查询出现系统错误", e);
      throw new CodeException("C0001", e);
    }
  }

  @RequestMapping({"/v_edit"})
  public String viewEditPage(String editId, ModelMap modelMap) throws CodeException {
    try {
      Organization organization = this.organizationService.getOrganizationById(editId);
      modelMap.put("organization", organization);
      modelMap.put("countChild", Integer.valueOf(this.organizationService.countChildRecordByParenId(organization.getId())));
      return this.jumpEditPagePath;
    } catch (Exception e) {
      logger.error("机构编辑出现系统错误", e);
      throw new CodeException("C0001", e);
    }
  }

  @RequestMapping({"/v_add"})
  public String viewAddPage(ModelMap modelMap) throws CodeException {
    Organization organization = new Organization();
    organization.setId(StringUtil.generateID());
    modelMap.put("organization", organization);
    return this.jumpAddPagePath;
  }

  @RequestMapping({"/v_detail"})
  public String view(String viewId, ModelMap modelMap, HttpServletRequest request) throws CodeException {
    try {
      Organization organization = this.organizationService.getOrganizationById(viewId);
      modelMap.put("organization", organization);
      return this.jumpDetailPagePath;
    } catch (Exception e) {
      logger.error("查看机构详细出现系统错误", e);
      throw new CodeException("C0001", e);
    }
  }

  @RequestMapping({"/o_add"})
  public String add(ModelMap modelMap, @Valid Organization organization, BindingResult result, HttpServletRequest request) throws CodeException {
    OperationLog operationLog = OperationLogManager.getInstance(request, this.applicationName, "增加", organization.getCode(), "增加机构详细信息", null);

    operationLog.setOrganizationId(SessionManager.getManager(request).getOrganizationId());
    try
    {
      List list;
      if (result.hasErrors()) {
        StringBuffer buffer = new StringBuffer();
        list = result.getAllErrors();
        Iterator iterator = list.iterator();
        while (iterator.hasNext()) {
          ObjectError error = (ObjectError)iterator.next();
          buffer.append(error.getDefaultMessage()).append("; ");
        }
        throw new CodeException("C0001", buffer.toString());
      }

      boolean resultInfo = this.organizationService.addOrganization(organization);
      modelMap.put("resultInfo", Boolean.valueOf(resultInfo));
      return this.jumplistdo;
    } catch (CodeException ce) {
      operationLog.setResult(10);
      logger.error("添加机构出现错误", ce);
      throw new CodeException(ce.getCode(), ce.getMessage());
    } catch (Exception e) {
      operationLog.setResult(10);
      logger.error("添加机构出现系统错误", e);
      throw new CodeException("C0001", e);
    } finally {
      this.operationLogService.addOperationLog(operationLog);
    }
  }

  @RequestMapping({"/o_edit"})
  public String edit(ModelMap modelMap, @Valid Organization organization, BindingResult result, HttpServletRequest request) throws CodeException {
    OperationLog operationLog = OperationLogManager.getInstance(request, this.applicationName, "修改", organization.getCode(), "修改机构详细信息", null);

    operationLog.setOrganizationId(SessionManager.getManager(request).getOrganizationId());
    try
    {
      List list;
      if (result.hasErrors()) {
        StringBuffer buffer = new StringBuffer();
        list = result.getAllErrors();
        Iterator iterator = list.iterator();
        while (iterator.hasNext()) {
          ObjectError error = (ObjectError)iterator.next();
          buffer.append(error.getDefaultMessage()).append("; ");
        }
        throw new CodeException("C0001", buffer.toString());
      }

      boolean resultInfo = this.organizationService.update(organization);
      modelMap.put("resultInfo", Boolean.valueOf(resultInfo));
      return this.jumplistdo;
    } catch (CodeException ce) {
      operationLog.setResult(10);
      logger.error("编辑机构出现错误", ce);
      throw new CodeException(ce.getCode(), ce.getMessage());
    } catch (Exception e) {
      operationLog.setResult(10);
      logger.error("编辑机构信息出现系统错误", e);
      throw new CodeException("C0001", e);
    } finally {
      this.operationLogService.addOperationLog(operationLog);
    }
  }

  @RequestMapping({"/o_delete"})
  public String delete(String deleteId, String code, ModelMap modelMap, HttpServletRequest request) throws CodeException {
    OperationLog operationLog = OperationLogManager.getInstance(request, this.applicationName, "删除", code, "删除机构详细信息", null);
    operationLog.setOrganizationId(SessionManager.getManager(request).getOrganizationId());
    try
    {
      if (this.organizationService.countAdminUserRecord(deleteId) > 0) {
        throw new CodeException("C7003", "存在管理员隶属于该组织机构,无法删除");
      }
      if (this.organizationService.countUsersRecord(deleteId) > 0) {
        throw new CodeException("C7004", "存在用户隶属于该组织机构,无法删除");
      }
      if (this.organizationService.countSealImageRecord(deleteId) > 0) {
        throw new CodeException("C7005", "存在印模隶属于该组织机构,无法删除");
      }
      if (this.organizationService.countSealRecord(deleteId) > 0) {
        throw new CodeException("C7006", "存在印章隶属于该组织机构,无法删除");
      }
      if (this.organizationService.countSealCertRecord(deleteId) > 0) {
        throw new CodeException("C7007", "存在印章证书隶属于该组织机构,无法删除");
      }
      if (this.organizationService.countChildRecordByParenId(deleteId) > 0) {
        throw new CodeException("C7008", "存在子机构隶属于该组织机构,无法删除");
      }

      int count = this.sealImageService.getCountByOrganizationId(deleteId);
      if (count > 0) {
        throw new CodeException("C7001", "待删除的企业已申请印模,无法删除");
      }

      boolean resultInfo = this.organizationService.deleteById(deleteId);
      modelMap.put("resultInfo", Boolean.valueOf(resultInfo));
      return this.jumplistdo;
    } catch (CodeException ce) {
      operationLog.setResult(10);
      logger.error("删除机构信息出现错误", ce);
      throw new CodeException(ce.getCode(), ce.getMessage());
    } catch (Exception e) {
      operationLog.setResult(10);
      logger.error("删除机构信息出现系统错误", e);
      throw new CodeException("C0001", e);
    } finally {
      this.operationLogService.addOperationLog(operationLog);
    }
  }
}